//
//  WZOriginView.swift
//  weibo
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit

class WZOriginView: UIView {
    let CellMargin = 10
    var contrain: Constraint?
    var cellViewModel: WZCellViewModel?{
        didSet{
            
            
            let photoImageURL = NSURL(string: cellViewModel!.model?.user?.profile_image_url ?? "")
            photoImageView.sd_setImageWithURL(photoImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            
            nameLable.text = cellViewModel?.model?.user?.screen_name
            //MARK: 设置时间和微博来源
                        timeLable.text = cellViewModel?.creatAtTime(cellViewModel?.model?.created_at)
            //            print(cellViewModel?.model?.created_at)
            
            sourceLable.text = cellViewModel?.realSource(cellViewModel?.model?.source ?? "")
           
            //            print(cellViewModel?.model?.source)
            contantLable.text = cellViewModel?.model?.text
            levelImageView.image = cellViewModel?.levelImage
            veriftyImageView.image = cellViewModel?.veriftyImage
            
//            判断是否有配图
            
            contrain?.uninstall()

            if let  pictures = cellViewModel?.model?.pic_urls where cellViewModel?.model?.pic_urls?.count > 0{//有配图
                
                  pictureOriginView.hidden = false
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.contrain = make.bottom.equalTo(pictureOriginView.snp_bottom).constraint
                })
                
                
               
//            定义一个[WZPictureModel]类型的数组赋值给配图
                    var pictureList = [WZCellViewModel]()
                    for pictureViewModel in pictures{
                        let cellViewModel = WZCellViewModel()
                        
                    cellViewModel.pictureViewModel = pictureViewModel
                        
                        pictureList.append(cellViewModel)
                    }
              pictureOriginView.pictureList = pictureList
              
            
            }else{//没有配图
                
                pictureOriginView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.contrain = make.bottom.equalTo(contantLable.snp_bottom).constraint
                })
            }
            
            
            
        }
    }
 
    override   init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
  
    private  func setupUI(){
        addSubview(photoImageView)
        addSubview(veriftyImageView)
        addSubview(nameLable)
        addSubview(levelImageView)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(contantLable)
        addSubview(pictureOriginView)
        
         //    MARK: 设置原型微博的约束
    photoImageView.snp_makeConstraints { (make) -> Void in
        make.top.equalTo(self.snp_top).offset(CellMargin)
        make.left.equalTo(self.snp_left).offset(CellMargin)
        make.size.equalTo(CGSizeMake(40, 40))
    }
        
        //    MARK: 认证图片约束
        veriftyImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
            //        make.size.equalTo(CGSizeMake(CGFloat(CellMargin), CGFloat(CellMargin)))
        }
        
        nameLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.top.equalTo(photoImageView.snp_top)
        }
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLable.snp_right).offset(CellMargin)
            make.centerY.equalTo(nameLable.snp_centerY)
            //        make.size.equalTo(CGSizeMake(CGFloat(CellMargin), CGFloat(CellMargin)))
            
        }
        timeLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(CellMargin)
            make.bottom.equalTo(photoImageView.snp_bottom)
        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLable.snp_right).offset(CellMargin).offset(CellMargin/2)
            make.bottom.equalTo(timeLable.snp_bottom)
        }
        
        contantLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(CellMargin)
            make.width.equalTo(UIScreen.mainScreen().bounds.width - 2*CGFloat(CellMargin))
            make.top.equalTo(photoImageView.snp_bottom).offset(CellMargin)
//            make.bottom.equalTo(self.snp_bottom)
            
        }
        
//        MARK:配图约束
//        
        pictureOriginView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contantLable.snp_bottom).offset(CellMargin)
            make.left.equalTo(self.snp_left).offset(CellMargin)
//            make.right.equalTo(self.snp_right).offset(10)
//            make.size.equalTo(CGSizeMake(100, 100))
          
           
        }
        
//  //       MARK: 高速view底部在哪也可以在配图设置
        self.snp_makeConstraints { (make) -> Void in
//         self.contrain = make.bottom.equalTo(pictureOriginView.snp_bottom).constraint
            make.bottom.equalTo(pictureOriginView.snp_bottom)
        }
    }
    
    
    
//    MARK:懒加载
    lazy var photoImageView: UIImageView = {
        let image = UIImageView()
        image.image = UIImage(named: "avatar_default_big")
        return image
    }()

    lazy var veriftyImageView: UIImageView = {
    let image = UIImageView()
        image.image = UIImage(named: "avatar_enterprise_vip")
        return image
    
    }()
    lazy var nameLable: UILabel = {
    let lable = UILabel()
        lable.text = "jhsadhashd"
        lable.font = UIFont.systemFontOfSize(15)
        return lable
    
    }()
    lazy var levelImageView: UIImageView = {
    
    let image = UIImageView()
        image.image = UIImage(named: "common_icon_membership_level4")
        
        return image
    }()
    
    lazy var timeLable: UILabel = {
        let lable = UILabel()
        lable.text = "事件"
        lable.textColor = UIColor.orangeColor()
        lable.font = UIFont.systemFontOfSize(13)
        return  lable
        
    }()
    lazy var sourceLable: UILabel = {
       let lable = UILabel()
        lable.text = "微博来源"
        lable.font = UIFont.systemFontOfSize(13)
        
        return lable
    }()
    lazy var contantLable: UILabel = {
    
    let lable = UILabel()
        lable.text = "微博内容jas活动看哈撒老师看见的垃圾索朗多吉阿拉斯加的垃圾啊数据的垃圾数量的加拉加斯的垃圾阿萨德将拉开建设的垃圾啊谁来当机立断家里撒娇"
        lable.numberOfLines = 0
        lable.font = UIFont.systemFontOfSize(13)
//        lable.textColor = UIColor.redColor()
        return lable
    
    }()
    //    MARK:原创微博的配图的懒加载
    lazy var pictureOriginView:WZPictureView = WZPictureView()
}



